"""
@name : 正则-作业
@author : chenwenying
@projectname: 正则表达式
@time : 2022/8/12
"""

# 作业：爬取三创官网的图片
# import requests
# import re
#
# url = "https://www.sanchuangedu.cn/"
# result = requests.get(url)
# html_text = result.text
# photo = re.findall(r"(?<=src=).*\.(?:png|jpg)", html_text)
# # print(photo)
# for i in photo:
#     print(i)
#     with open(f"{i}", "wb") as fp:
#         p = requests.get(url+i)
#         # print(p)
#         fp.write(p.content)


# 作业：使用 多进程|多线程|进程池 局网扫描
import IPy
import time
from pythonping import ping
from multiprocessing import Pool


def ip_text(ip):
    res = ping(ip, timeout=1)
    time.sleep(2)
    if res.success():
        print(str(ip)+':' + "up")
        p_lst_used.append(str(ip))
    else:
        print(str(ip)+":"+"down")
        p_lst_no.append(str(ip))


ip_net = '192.168.1.0/24'
ips = IPy.IP(ip_net)
p_lst_used = []
p_lst_no = []

if __name__ == "__main__":
    pool = Pool(processes=4, maxtasksperchild=4)
    for ip in ips:
        pool.apply_async(func=ip_text, args=(str(ip),))
    pool.close()
    pool.join()

    print("end...")
    print(p_lst_used)
    print(p_lst_no)
